/**
 * 
 */
package com.alstudio.view.absadapter;

import java.util.ArrayList;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.pojo.module.user.User;
import com.alstudio.utils.formater.age.ALAgeFormatter;
import com.alstudio.utils.formater.sex.ALSexFormatter;
import com.alstudio.utils.formater.time.ALTimeUtils;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class BaseUserListAdapter extends ViewAdapter {

	private ArrayList<User> data = new ArrayList<User>();

	// 是否启用最右边的imageview
	private boolean isAddonIconUse = false;

	private int addonIconRes = -1;

	private boolean showDelete = false;

	private int layoutRes = -1;

	private boolean isNickOnly = false;

	public interface AddOnIconClickListener {
		public void onAddOnIconClick(Object obj);
	}

	private AddOnIconClickListener addOnIconClickListener;

	/**
	 * 
	 */
	public BaseUserListAdapter(ArrayList<User> data) {
		// TODO Auto-generated constructor stub
		this.data = data;
	}

	/**
	 * @param layoutRes
	 *            the layoutRes to set
	 */
	public void setLayoutRes(int layoutRes) {
		this.layoutRes = layoutRes;
	}

	private class ViewHolder {
		public TextView nick_name, time, location_age, sig, delet;
		public ImageView image, sex, vauth, addon_icon;
		public View time_layout;
	}

	public void setNickOnly(boolean isNickOnly) {
		this.isNickOnly = isNickOnly;
	}

	public boolean isNickOnly() {
		return isNickOnly;
	}

	/**
	 * @param isAddonIconUse
	 *            the isAddonIconUse to set
	 */
	public void setAddonIconUse(boolean isAddonIconUse) {
		this.isAddonIconUse = isAddonIconUse;
	}

	/**
	 * @return the isAddonIconUse
	 */
	public boolean isAddonIconUse() {
		return isAddonIconUse;
	}

	/**
	 * @param addonIconRes
	 *            the addonIconRes to set
	 */
	public void setAddonIconRes(int addonIconRes) {
		this.addonIconRes = addonIconRes;
	}

	/**
	 * @return the addonIconRes
	 */
	public int getAddonIconRes() {
		return addonIconRes;
	}

	/**
	 * @param showDelete
	 *            the showDelete to set
	 */
	public void setShowDelete(boolean showDelete) {
		this.showDelete = showDelete;
	}

	/**
	 * @return the showDelete
	 */
	public boolean isShowDelete() {
		return showDelete;
	}

	/**
	 * @param addOnIconClickListener
	 *            the addOnIconClickListener to set
	 */
	public void setAddOnIconClickListener(
			AddOnIconClickListener addOnIconClickListener) {
		this.addOnIconClickListener = addOnIconClickListener;
	}

	/**
	 * @return the addOnIconClickListener
	 */
	public AddOnIconClickListener getAddOnIconClickListener() {
		return addOnIconClickListener;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final ViewHolder holder;
		if (convertView == null) {
			int reslayout = R.layout.main_discovery_single_item;
			if (layoutRes != -1) {
				reslayout = layoutRes;
			}
			view = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater()
					.inflate(reslayout, parent, false);

			holder = new ViewHolder();
			holder.time = (TextView) view.findViewById(R.id.time);
			holder.nick_name = (TextView) view.findViewById(R.id.nick_name);
			holder.location_age = (TextView) view
					.findViewById(R.id.location_age);
			holder.sex = (ImageView) view.findViewById(R.id.sex);
			holder.vauth = (ImageView) view.findViewById(R.id.vauth);

			holder.image = (ImageView) view.findViewById(R.id.icon);
			holder.sig = (TextView) view.findViewById(R.id.sig);
			holder.addon_icon = (ImageView) view.findViewById(R.id.addon_icon);

			holder.delet = (TextView) view.findViewById(R.id.delete_bt);

			holder.time_layout = view.findViewById(R.id.time_layout);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		setupViewItem(holder, position);
		// if(showDelete){
		// view.startAnimation(AnimationUtils.loadAnimation(ALLocalEnv.getLocalEnv(),
		// R.anim.activity_left_to_right));
		// }
		return view;
	}

	private void setupViewItem(ViewHolder holder, int pos) {
		User u = data.get(pos);
		holder.nick_name.setText(u.getNick());
		
		ALLocalEnv.getLocalEnv().displayImagePluginServerlet(
				u.getImageFileId(), holder.image,
				ALLocalEnv.getLocalEnv().getRoundImageDisplayOption());

		if (isNickOnly) {
			holder.location_age.setVisibility(View.GONE);
			holder.sig.setVisibility(View.GONE);
			holder.time_layout.setVisibility(View.GONE);
			holder.delet.setVisibility(View.GONE);
			holder.sex.setVisibility(View.GONE);
			holder.vauth.setVisibility(View.GONE);
			holder.addon_icon.setVisibility(View.GONE);
			return;
		}

		holder.time.setText(ALTimeUtils.formatTime(u.getLastLoginTimeStamp()));

		holder.location_age.setText(ALAgeFormatter.getAgeLocation(u.getAge(),
				u.getLocation()));
		// 根据性别，设置颜色
		holder.location_age.setTextColor(ALSexFormatter.getSexTxtColor(u
				.getSex()));

		holder.sex.setImageResource(ALSexFormatter.transSexBigDrawable(u
				.getSex()));
		if (u.isVauthed()) {
			holder.vauth.setImageResource(R.drawable.find_user_icon_vip);
		} else {
			holder.vauth.setImageBitmap(null);
		}

		// 签名
		holder.sig.setText(u.getSignature());

		if (showDelete) {
			holder.delet.setVisibility(View.VISIBLE);
			// holder.delet.startAnimation(AnimationUtils.loadAnimation(ALLocalEnv.getLocalEnv(),
			// R.anim.activity_left_to_right));
			holder.time_layout.setVisibility(View.GONE);
			setupAddOnIconClickEvent(holder.delet, pos);
		} else {
			holder.delet.setVisibility(View.GONE);
			holder.time_layout.setVisibility(View.VISIBLE);
			holder.delet.setOnClickListener(null);
		}

		// 右边图标
		if (addonIconRes != -1 && isAddonIconUse && u.getIsfriend()) {
			holder.addon_icon.setImageResource(addonIconRes);
		} else {
			holder.addon_icon.setImageResource(0);

		}

	}

	private void setupAddOnIconClickEvent(View view, Object data) {
		view.setTag(data);
		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (addOnIconClickListener != null) {
					addOnIconClickListener.onAddOnIconClick(v.getTag());
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.view.absadapter.ViewAdapter#getRealCount()
	 */
	@Override
	public int getRealCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

}
